package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    @Autowired
    CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize, String name) {
        Page<Setmeal> pg = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pg, queryWrapper);
        Page<SetmealDto> pg1 = new Page<>();
        BeanUtils.copyProperties(pg, pg1, "records");
        List<Setmeal> records = pg.getRecords();
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            Long categoryId = record.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            String name1 = byId.getName();
            setmealDto.setCategoryName(name1);
            list.add(setmealDto);
        }
        return R.success(pg1.setRecords(list));

    }

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithSetmeal(setmealDto);
        return R.success("添加成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateByIds(@PathVariable Integer status, @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.set(Setmeal::getStatus, status);
        lqw.in(Setmeal::getId, ids);
        setmealService.update(lqw);
        return R.success("修改成功");
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteByIds(Long[] ids) {
        boolean b = setmealService.removeByIdsWithSetmeal(ids);
        return b ? R.success("删除成功") : R.error("删除失败");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id) {
        return R.success(setmealService.getByIdWithSetmeal(id));
    }

    @PutMapping
    public R<String> updateById(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithSetmeal(setmealDto);
        return R.success("修改成功");
    }

    //查询套餐
    ///setmeal/list?categoryId=1413342269393674242&status=1
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> select(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus, 1);
        return R.success(setmealService.list(lqw));
    }
}
